package com.umerclass.ui.home;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umerclass.AppDroid;
import com.umerclass.R;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.frame.ui.barlibrary.ImmersionBar;
import com.umerclass.ui.action.ActionFragment;
import com.umerclass.ui.shop.ShopFragment;
import com.umerclass.ui.user.UserFragment;
import com.umerclass.ui.views.CustomViewPager;
import com.umerclass.utils.Constants;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.OnClick;
import static android.graphics.Color.parseColor;

public class HomeActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.viewPager)
    CustomViewPager viewPager;
    @BindView(R.id.bottom_bar_text_1)
    TextView bottomBarText1;
    @BindView(R.id.bottom_bar_image_1)
    ImageView bottomBarImage1;
    @BindView(R.id.bottom_bar_1_btn)
    RelativeLayout bottomBar1Btn;
    @BindView(R.id.bottom_bar_text_2)
    TextView bottomBarText2;
    @BindView(R.id.bottom_bar_image_2)
    ImageView bottomBarImage2;
    @BindView(R.id.bottom_bar_2_btn)
    RelativeLayout bottomBar2Btn;
    @BindView(R.id.bottom_bar_text_3)
    TextView bottomBarText3;
    @BindView(R.id.bottom_bar_image_3)
    ImageView bottomBarImage3;
    @BindView(R.id.bottom_bar_3_btn)
    RelativeLayout bottomBar3Btn;
    @BindView(R.id.bottom_bar_text_4)
    TextView bottomBarText4;
    @BindView(R.id.bottom_bar_image_4)
    ImageView bottomBarImage4;
    @BindView(R.id.bottom_bar_4_btn)
    RelativeLayout bottomBar4Btn;
    private long firstTime = 0;
    private ArrayList<Fragment> mFragments;
    private IWXAPI api;

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .navigationBarDarkIcon(true)
                .navigationBarEnable(true)
                .navigationBarColor(R.color.white)
                .navigationBarDarkIcon(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_home;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        long secondTime = System.currentTimeMillis();
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                AppDroid.getInstance().exitClient();
            } else {
                Toast.makeText(getApplicationContext(), "再按一次返回键退出", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void init() {

        /**
         * 将该app注册到微信
         */
        checkPermission();
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false);
        api.registerApp(Constants.APP_ID);

        viewPager.addOnPageChangeListener(this);
        mFragments = new ArrayList<>();
        ShopFragment shopFragment = new ShopFragment();
        HomeFragment homeFragment = new HomeFragment();
        ActionFragment actionFragment = new ActionFragment();
        UserFragment userFragment = new UserFragment();

        mFragments.add(shopFragment);
        mFragments.add(homeFragment);
        mFragments.add(actionFragment);
        mFragments.add(userFragment);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
        viewPager.setOffscreenPageLimit(4);
        viewPager.setCurrentItem(0);
    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.bottom_bar_1_btn, R.id.bottom_bar_2_btn,
            R.id.bottom_bar_3_btn,R.id.bottom_bar_4_btn})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.bottom_bar_1_btn:
                setSelectStatus(0);
                viewPager.setCurrentItem(0);
                break;
            case R.id.bottom_bar_2_btn:
                setSelectStatus(1);
                viewPager.setCurrentItem(1);
                break;
            case R.id.bottom_bar_3_btn:
                setSelectStatus(2);
                viewPager.setCurrentItem(2);
                break;
            case R.id.bottom_bar_4_btn:
                setSelectStatus(3);
                viewPager.setCurrentItem(3);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private class MyAdapter extends FragmentPagerAdapter {
        MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }

    @SuppressLint("ResourceAsColor")
    private void setSelectStatus(int index) {
        switch (index) {
            case 0:
                //图片点击选择变换图片，颜色的改变，其他变为原来的颜色，并保持原有的图片
                bottomBarImage1.setImageResource(R.drawable.home_press);
                bottomBarText1.setTextColor(parseColor("#AD1F26"));
                //其他的文本颜色不变
                bottomBarText2.setTextColor(parseColor("#888888"));
                bottomBarText3.setTextColor(parseColor("#888888"));
                bottomBarText4.setTextColor(parseColor("#888888"));
                //图片也不变
                bottomBarImage2.setImageResource(R.drawable.study_normal);
                bottomBarImage3.setImageResource(R.drawable.shop_naomal);
                bottomBarImage4.setImageResource(R.drawable.user_naomal);
                break;
            case 1:
                //图片点击选择变换图片，颜色的改变，其他变为原来的颜色，并保持原有的图片
                bottomBarImage2.setImageResource(R.drawable.study_press);
                bottomBarText2.setTextColor(parseColor("#AD1F26"));
                //其他的文本颜色不变
                bottomBarText1.setTextColor(parseColor("#888888"));
                bottomBarText3.setTextColor(parseColor("#888888"));
                bottomBarText4.setTextColor(parseColor("#888888"));
                //图片也不变
                bottomBarImage1.setImageResource(R.drawable.home_normal);
                bottomBarImage3.setImageResource(R.drawable.shop_naomal);
                bottomBarImage4.setImageResource(R.drawable.user_naomal);
                break;
            case 2:
                //图片点击选择变换图片，颜色的改变，其他变为原来的颜色，并保持原有的图片
                bottomBarImage3.setImageResource(R.drawable.shop_press);
                bottomBarText3.setTextColor(parseColor("#AD1F26"));
                //其他的文本颜色不变
                bottomBarText2.setTextColor(parseColor("#888888"));
                bottomBarText1.setTextColor(parseColor("#888888"));
                bottomBarText4.setTextColor(parseColor("#888888"));
                //图片也不变
                bottomBarImage2.setImageResource(R.drawable.study_normal);
                bottomBarImage1.setImageResource(R.drawable.home_normal);
                bottomBarImage4.setImageResource(R.drawable.user_naomal);
                break;
            case 3:
                //图片点击选择变换图片，颜色的改变，其他变为原来的颜色，并保持原有的图片
                bottomBarImage4.setImageResource(R.drawable.user_press);
                bottomBarText4.setTextColor(parseColor("#AD1F26"));
                //其他的文本颜色不变
                bottomBarText1.setTextColor(parseColor("#888888"));
                bottomBarText2.setTextColor(parseColor("#888888"));
                bottomBarText3.setTextColor(parseColor("#888888"));
                //图片也不变
                bottomBarImage1.setImageResource(R.drawable.home_normal);
                bottomBarImage2.setImageResource(R.drawable.study_normal);
                bottomBarImage3.setImageResource(R.drawable.shop_naomal);
                break;
            default:
                break;
        }
    }

    private void checkPermission() {
        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    Constants.PERMISSIONS_REQUEST_STORAGE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case Constants.PERMISSIONS_REQUEST_STORAGE: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                } else {
                    Toast.makeText(HomeActivity.this, "请授予相关权限!", Toast.LENGTH_LONG).show();
                }
                return;
            }
        }
    }
}
